#region Librerias

using System;
using ESF.GTK.Elements;
using System.ComponentModel;
using ESF.SFT.FileUploader.Presenter.General;
using Gtk;
using ESF.SFT.Proxies;
using ESF.GTK;
using ESF.SFT.FileUploader.SharedResources;
using ESF.SFT.FileUploader.Presenter.FileReceiverGeneral;
using ESF.General.Entity;
using System.Collections.Generic;
using ESF.General;
using System.Threading;

#endregion


namespace ESF.SFT.FileUploaderGTK
{
	public partial class SplashScreen 
		: BaseGTKWindow<SplashScreenPresenter, ISplashScreenViewer>, ISplashScreenViewer
	{
		#region Members
		
		private BackgroundWorker _Bg;
		
		#endregion


		#region Constructor

		public SplashScreen () : base (WindowType.Toplevel)
		{
			_Presenter = new SplashScreenPresenter(this);
			
			Build();
			
			imgLogo.Pixbuf = GTKConverter.ImageToPixbuf(SharedImages.Upload_64x64);
			
			_Bg = new BackgroundWorker();
			
			_Bg.DoWork += delegate (object s, DoWorkEventArgs eg) { _Presenter.Load(); };
			_Bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_Bg_RunWorkerCompleted);
			_Bg.RunWorkerAsync();
		}

		#endregion


		#region Methods
		
		public void CaptureProxySettings()
		{
			if (Program.winICO == null)
			{
				Application.Invoke (delegate { Program.winICO = new InternetConnectionOptions (this); });
			}
			else
			{
				Application.Invoke (delegate { Program.winICO.Show (); });
			}
		}
		
		#endregion
		
		
		#region Window Events
		
		protected void OnDeleteEvent(object sender, DeleteEventArgs a)
		{
			CloseApplication ();
		}
		
		#endregion


		#region Background Worker Events

		protected void _Bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			if (e.Error != null) 
			{
				LogError (e.Error);

				CloseApplication();

				return;
			}

			Log.StopOps();

			// Para permitir ver el mensaje de completado
			Thread.Sleep(500);

			Application.Invoke(delegate 
			{ 
				Hide(); 

				Program.winSignIn = new SignInWindow (); 
			});
		}

		#endregion

		
		#region ISplashScreenViewer Fields
		
		public int LoadingProgress
		{
			set { SetFraction (prgProgressbar, value); }
		}
		
		public string ProductName
		{
			set { SetText (lblProductName, value); }
		}
		
		public string ProductVersion
		{
			set { SetText (lblProductVersion, value); }
		}
		
		public string PublisherName
		{
			set { SetText (lblPublisherName, value); }
		}
		
		public string CurrentStep
		{
			set { Log.CurrentStep = value; SetText (lblLoadingMessage, value); }
		}

		public List<Couple<string, string>> Conf
		{
			set { Program.Conf = value; }
		}
		
		#endregion
	}		
}

